package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1、根据前端传过来的数据从数据库进行查询
        ShoppingCart cart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        List<ShoppingCart> results = shoppingCartMapper.queryByParams(cart);

        //2、判断是否有数据，如果有，证明该菜品或套餐以前添加过，number +1 即可
        if(ObjectUtil.isNotEmpty(results)){
            cart = results.get(0);
            shoppingCartMapper.incrementNumberById(cart.getId(),1);
        }else{
            //3、如果没有，证明该菜品或套餐以前没有添加过，新增数据至数据库
            //3.1、菜品ID不为空，证明现在添加的是菜品
            if(ObjectUtil.isNotEmpty(shoppingCartDTO.getDishId())){
                DishVO dishVO = dishMapper.queryById(shoppingCartDTO.getDishId());
                cart.setName(dishVO.getName());
                cart.setImage(dishVO.getImage());
                cart.setAmount(dishVO.getPrice());
            }else{
                //3.2、菜品ID为空，证明现在添加的是套餐
                SetmealVO setmealVO = setmealMapper.queryById(shoppingCartDTO.getSetmealId());
                cart.setName(setmealVO.getName());
                cart.setImage(setmealVO.getImage());
                cart.setAmount(setmealVO.getPrice());
            }
            cart.setNumber(1);
            shoppingCartMapper.insert(cart);
        }
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return this.shoppingCartMapper.queryByParams(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删减购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //1、判断该用户的购物车中是否有该口味的菜品或套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> cartList = this.shoppingCartMapper.queryByParams(shoppingCart);

        //2、如果有，进行删减操作
        if(ObjectUtils.isNotEmpty(cartList)){
            shoppingCart = cartList.get(0);
            //如果菜品数量大于1，进行数量-1更新
            if(shoppingCart.getNumber() > 1){
                this.shoppingCartMapper.decrementNumberById(shoppingCart.getId(),1);
            }else{
                //如果菜品数量小于等于1，直接将记录进行删除
                this.shoppingCartMapper.deleteById(shoppingCart.getId());
            }
        }
    }
}
